#include <QString>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include "qmytableview.h"
#include "cglobal.h"

QMyTableView::QMyTableView(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	setStyleSheetFile();
	//预初始化
	preinit();
	//连接槽函数
	initConnect();
	//单选状态下隐藏两个选择框
	ui.checkBoxChooseAll->hide();
	ui.checkBoxChoosePage->hide();
}

QMyTableView::~QMyTableView()
{}

void QMyTableView::UpdateStatus()
{
	if (m_nCurrentPage == 1 && m_nCurrentPage != m_nPageCount)
	{
		ui.pushButtonPre->setEnabled(false);
		ui.pushButtonNext->setEnabled(true);
	}
	else if (m_nCurrentPage == m_nPageCount && m_nCurrentPage != 1)
	{
		ui.pushButtonPre->setEnabled(true);
		ui.pushButtonNext->setEnabled(false);
	}
	else if (m_nCurrentPage == 1 && m_nPageCount == 1)
	{
		ui.pushButtonPre->setEnabled(false);
		ui.pushButtonNext->setEnabled(false);
	}
	else
	{
		ui.pushButtonPre->setEnabled(true);
		ui.pushButtonNext->setEnabled(true);
	}
}

void QMyTableView::UpdateStatus(int nCount)
{
	m_nCurrentPage = 1;//当前的页数
	m_nTotalCount = nCount;//记录总数
	if (nCount % m_nPerPageCount == 0)
	{
		m_nPageCount = nCount / m_nPerPageCount;
	}
	else
	{
		m_nPageCount = nCount / m_nPerPageCount + 1;
	}
	ui.label->setText(QString::fromLocal8Bit("1/%1页").arg(m_nPageCount));
	ui.comboBox->clear();
	for (int i = 0; i < m_nPageCount; i++)
	{
		ui.comboBox->addItem(QString::number(i + 1));
	}

	if (m_nCurrentPage == 1 && m_nCurrentPage != m_nPageCount)
	{
		ui.pushButtonPre->setEnabled(false);
		ui.pushButtonNext->setEnabled(true);
	}
	else if (m_nCurrentPage == m_nPageCount && m_nCurrentPage != 1)
	{
		ui.pushButtonPre->setEnabled(true);
		ui.pushButtonNext->setEnabled(false);
	}
	else if (m_nCurrentPage == 1 && m_nPageCount == 1)
	{
		ui.pushButtonPre->setEnabled(false);
		ui.pushButtonNext->setEnabled(false);
	}
	else
	{
		ui.pushButtonPre->setEnabled(true);
		ui.pushButtonNext->setEnabled(true);
	}
}

void QMyTableView::ShowPage(int nPage)
{
	for (int i = 0; i < m_pModel->rowCount(); i++)
	{
		ui.tableView->setRowHidden(i, false);
	}

	//需要隐藏的行
	int nHidden = (nPage - 1) * m_nPerPageCount;
	for (int i = 0; i < nHidden; i++)
	{	
		ui.tableView->setRowHidden(i, true);
	}
	for (int i = nHidden; i < nPage * m_nPerPageCount; i++)
	{
		ui.tableView->setRowHidden(i, false);
	}
	for (int i = nPage * m_nPerPageCount; i < m_nTotalCount; i++)
	{
		ui.tableView->setRowHidden(i, true);
	}
}

void QMyTableView::tableOpenMultiChoice()
{
	//显示选择框
	ui.checkBoxChooseAll->show();
	ui.checkBoxChoosePage->show();

	if (m_pModel->columnCount() > 0 && QString::fromLocal8Bit("") != m_pModel->horizontalHeaderItem(0)->text())
	{
		QList<QStandardItem*> listCheckBox;
		for (int i = 0; i < m_pModel->rowCount(); i++)
		{
			QStandardItem* item = new QStandardItem();
			item->setCheckable(true);
			item->setTextAlignment(Qt::AlignCenter);
			listCheckBox.append(item);
		}

		m_pModel->insertColumn(0, listCheckBox);
		ui.tableView->setColumnWidth(0, 20);
		m_pModel->setHorizontalHeaderLabels({ QString::fromLocal8Bit("") });
	}

}

void QMyTableView::tableCloseMultiChoice()
{
	//隐藏选择框
	ui.checkBoxChooseAll->hide();
	ui.checkBoxChoosePage->hide();

	if (m_pModel->columnCount() > 0 && QString::fromLocal8Bit("") == m_pModel->horizontalHeaderItem(0)->text())
	{
		m_pModel->removeColumn(0);
	}
}

bool QMyTableView::returnCheckedRowNo(QVector<int>* vecChecked)
{
	for (int i = 0; i < m_pModel->rowCount(); i++)
	{
		if (Qt::Checked == m_pModel->item(i, 0)->checkState())
		{
			vecChecked->push_back(i);
		}
	}

	return true;
}

void QMyTableView::setPerPageCount(int nPerPageCount)
{
	m_nPerPageCount = nPerPageCount;
}

void QMyTableView::exportTableToCSV(QString &sFilePath, QString &sCloTitle, QString &sContentEx)
{
	// 打开文件以写入
	QFile file(sFilePath);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
	{
		QMessageBox::warning(nullptr, A2T("错误"), A2T("无法打开文件以写入."));
		return;
	}

	QTextStream out(&file);

	//写入附加内容
	out << sContentEx << "\n";

	if (sCloTitle == "")
	{
		// 写入表头
		for (int column = 0; column < m_pModel->columnCount(); ++column)
		{
			if (column > 0)
				out << ","; // 列之间用逗号分隔
			out << m_pModel->headerData(column, Qt::Horizontal).toString();
		}
	}
	else
	{
		out << sCloTitle;
	}
	out << "\n"; // 换行

	// 写入数据行
	for (int row = 0; row < m_pModel->rowCount(); ++row)
	{
		for (int column = 0; column < m_pModel->columnCount(); ++column)
		{
			if (column > 0)
				out << ","; // 列之间用逗号分隔
			out << m_pModel->data(m_pModel->index(row, column)).toString();
		}
		out << "\n"; // 换行
	}

	file.close(); // 关闭文件
	QMessageBox::information(nullptr, A2T("成功"), A2T("表格数据已成功导出到") + sFilePath);
}

void QMyTableView::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void QMyTableView::preinit()
{
	m_nPerPageCount = 100;//默认每页显示100
	//设置字体
	//QFont font("song", 10);
	//ui.tableView->setFont(font);
	//设置表头可点击
	ui.tableView->horizontalHeader()->setSectionsClickable(true);
	//设置表头字体
	//ui.tableView->horizontalHeader()->setFont(QFont("song", 13));
	if (!ui.tableView->alternatingRowColors())
	{
		ui.tableView->setAlternatingRowColors(true);
	}
	//隐藏QTableView中的左边序号列
	ui.tableView->verticalHeader()->setVisible(false);
	
	//禁止编辑
	ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	//单选行
	ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	//模式为单选
	ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
	//行末尾自动拉伸填充
	ui.tableView->horizontalHeader()->setStretchLastSection(true);
	//初始化模型
	m_pModel = nullptr;
	m_pModel = new QStandardItemModel();
	ui.tableView->setModel(m_pModel);

	//ui.tableView->setSortingEnabled(true);
}

void QMyTableView::initConnect()
{
	connect(ui.pushButtonPre, SIGNAL(clicked()), this, SLOT(on_PrePageBtn()));
	connect(ui.pushButtonNext, SIGNAL(clicked()), this, SLOT(on_NextPageBtn()));
	connect(ui.comboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(on_CurrentChanged()));
	connect(ui.checkBoxChoosePage, &QCheckBox::stateChanged, this, &QMyTableView::slotCheckBoxChoosePageChecked);
	connect(ui.checkBoxChooseAll, &QCheckBox::stateChanged, this, &QMyTableView::slotCheckBoxChooseAllChecked);
	connect(ui.tableView->horizontalHeader(), &QHeaderView::sortIndicatorChanged, this, &QMyTableView::slotSortIndicatorChanged);
	connect(m_pModel, &QStandardItemModel::rowsInserted, this, &QMyTableView::slotTableViewRowInserted);
	connect(ui.tableView, &QTableView::clicked, this, &QMyTableView::slotTest);
}

void QMyTableView::on_NextPageBtn()
{
	m_nCurrentPage++;
	ui.comboBox->setCurrentIndex(m_nCurrentPage - 1);
	ui.label->setText(QString::fromLocal8Bit("%1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));
	ShowPage(m_nCurrentPage);
	UpdateStatus();
}

void QMyTableView::on_PrePageBtn()
{
	m_nCurrentPage--;
	ui.comboBox->setCurrentIndex(m_nCurrentPage - 1);
	ui.label->setText(QString::fromLocal8Bit("%1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));
	ShowPage(m_nCurrentPage);
	UpdateStatus();
}

void QMyTableView::on_CurrentChanged()
{
	m_nCurrentPage = ui.comboBox->currentIndex() + 1;
	ui.label->setText(QString::fromLocal8Bit("%1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));
	ShowPage(ui.comboBox->currentIndex() + 1);
	UpdateStatus();
}

//选择本页选择框槽函数
void QMyTableView::slotCheckBoxChoosePageChecked(int nCheckState)
{
	if (Qt::Checked == nCheckState)
	{
		//将选择全部的勾去掉
		ui.checkBoxChooseAll->setChecked(false);

		//将本页的全部勾上
		for (int i = 0; i < m_pModel->rowCount(); i++)
		{
			if (!ui.tableView->isRowHidden(i))
			{
				m_pModel->item(i, 0)->setCheckState(Qt::Checked);
			}
		}
	}
	else if (Qt::Unchecked == nCheckState)
	{
		//将本页的全部勾上
		for (int i = 0; i < m_pModel->rowCount(); i++)
		{
			if (!ui.tableView->isRowHidden(i))
			{
				m_pModel->item(i, 0)->setCheckState(Qt::Unchecked);
			}
		}
	}
}

//选择全部选择框槽函数
void QMyTableView::slotCheckBoxChooseAllChecked(int nCheckState)
{
	if (Qt::Checked == nCheckState)
	{
		//将选择页的勾去掉
		ui.checkBoxChoosePage->setChecked(false);

		//全部勾上
		for (int i = 0; i < m_pModel->rowCount(); i++)
		{
			m_pModel->item(i, 0)->setCheckState(Qt::Checked);
		}
	}
	else if (Qt::Unchecked == nCheckState)
	{
		//全部勾上
		for (int i = 0; i < m_pModel->rowCount(); i++)
		{
			m_pModel->item(i, 0)->setCheckState(Qt::Unchecked);
		}
	}
}

void QMyTableView::slotHorizontalHeaderClicked(int nIndex, Qt::SortOrder nOrder)
{
	//记录排列的列号与排序方式
	m_nSortCol = nIndex;
	m_nSortOrder = nOrder;
}

void QMyTableView::slotTableViewRowInserted()
{

}

void QMyTableView::slotSortIndicatorChanged(int nIndex, int nOrder)
{
	ui.tableView->sortByColumn(nIndex);
	//需要隐藏的行
	int nHidden = 0 * m_nPerPageCount;
	for (int i = 0; i < nHidden; i++)
	{
		ui.tableView->setRowHidden(i, true);
	}
	for (int i = nHidden; i < 1 * m_nPerPageCount; i++)
	{
		ui.tableView->setRowHidden(i, false);
	}
	for (int i = 1 * m_nPerPageCount; i < m_nTotalCount; i++)
	{
		ui.tableView->setRowHidden(i, true);
	}

	ui.label->setText(QString::fromLocal8Bit("1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));

}

void QMyTableView::slotTest(QModelIndex nIndex)
{


}
